package com.heer.gymnasium.module.main.views;

import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.util.Util;
import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.SignHelper;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseFragment;
import com.heer.gymnasium.base.BaseRefreshFragment;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.card.beans.CardResponseBean;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.main.beans.AdmissionBean;
import com.heer.gymnasium.module.my.beans.FaceBean;
import com.heer.gymnasium.module.my.views.FaceActivity;
import com.heer.gymnasium.module.order.beans.OrderResponseListBean;
import com.heer.gymnasium.module.reservation.views.TicketDetailActivity;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan on 2018/1/11.
 * 入场Fragment
 */

public class AdmissionFragment extends BaseRefreshFragment {

    private final static int RES_ID_ROOT_LAYOUT = R.layout.layout_admission;

    private final static int RES_ID_FACE = R.id.layout_admission_face_ll;
    private final static int RES_ID_LIST = R.id.layout_admission_card_lv;
    private final static int RES_ID_LIST_ITEM = R.layout.item_admission;
    private final static int RES_ID_LIST_ITEM_CARD_TYPE = R.id.item_admission_card_type_tv;
    private final static int RES_ID_LIST_ITEM_PANEL = R.id.item_admission_panel_ll;
    private final static int RES_ID_LIST_ITEM_LINE = R.id.item_admission_center_line_v;
    private final static int RES_ID_LIST_ITEM_CARD_TYPE_ICON = R.id.item_admission_card_type_iv;
    private final static int RES_ID_LIST_ITEM_CARD_NAME = R.id.item_admission_name_tv;
    private final static int RES_ID_LIST_ITEM_ADDRESS_IV = R.id.item_admission_address_iv;
    private final static int RES_ID_LIST_ITEM_ADDRESS_TV = R.id.item_admission_address_tv;
    private final static int RES_ID_LIST_ITEM_TIME_IV = R.id.item_admission_time_iv;
    private final static int RES_ID_LIST_ITEM_TIME_TV = R.id.item_admission_time_tv;
    private final static int RES_ID_LIST_ITEM_ENTER_IV = R.id.item_admission_enter_iv;
    private final static int RES_ID_LIST_ITEM_ICON = R.id.item_admission_iv;


    private final static int DRAWABLE_YELLOW_BG = R.drawable.ticket_yellow_bg;
    private final static int DRAWABLE_BLACK_BG = R.drawable.ticket_black_bg;
    private final static int DRAWABLE_YELLOW_LINE = R.drawable.yellow_bg;
    private final static int DRAWABLE_BLACK_LINE = R.color.black;
    private final static int DRAWABLE_BLACK_TICKET = R.drawable.ic_card_gray;
    private final static int DRAWABLE_WHITE_TICKET = R.drawable.ic_ticket;
    private final static int DRAWABLE_BLACK_LOCATE = R.drawable.ic_location_gray;
    private final static int DRAWABLE_WHITE_LOCATE = R.drawable.ic_location_white;
    private final static int DRAWABLE_BLACK_TIME = R.drawable.ic_time_gray;
    private final static int DRAWABLE_WHITE_TIME = R.drawable.ic_time_white;
    private final static int DRAWABLE_BLACK_ANCHOR = R.drawable.ic_arrow_right;
    private final static int DRAWABLE_WHITE_ANCHOR = R.drawable.ic_arrow_enter;


    @BindView(RES_ID_REFRESH_LAYOUT)
    SwipyRefreshLayout refreshLayout;
    @BindView(RES_ID_FACE)
    View facePanel;
    @BindView(RES_ID_LIST)
    ListView listView;

    private QuickTypeAdapter<AdmissionBean> admissionAdapter;
    private ArrayList<AdmissionBean> admissionList;
    private Activity $this;

    @Override
    protected int setConvertView() {
        return RES_ID_ROOT_LAYOUT;
    }

    @Override
    protected void init() {
        super.init();
        $this = this.getActivity();
        admissionList = new ArrayList<>();
        admissionAdapter = new QuickTypeAdapter<AdmissionBean>(getContext(), admissionList, RES_ID_LIST_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, final AdmissionBean data, int type, int pos) {
                if (TextUtils.equals(Type.BUY_CARD_TYPE, data.getAdmissionType()) || TextUtils.equals(Type.COACH_TYPE, data.getAdmissionType())) {
                    //会员卡
                    holder.setVisibility(RES_ID_LIST_ITEM_CARD_TYPE, View.VISIBLE);
                    holder.setBg(RES_ID_LIST_ITEM_PANEL, DRAWABLE_YELLOW_BG);
                    holder.setBg(RES_ID_LIST_ITEM_LINE, DRAWABLE_BLACK_LINE);
                    holder.setImg(RES_ID_LIST_ITEM_CARD_TYPE_ICON, DRAWABLE_BLACK_TICKET);
                    holder.setTextColor(RES_ID_LIST_ITEM_CARD_NAME, Color.BLACK);
                    holder.setImg(RES_ID_LIST_ITEM_ADDRESS_IV, DRAWABLE_BLACK_LOCATE);
                    holder.setTextColor(RES_ID_LIST_ITEM_ADDRESS_TV, Color.BLACK);
                    holder.setImg(RES_ID_LIST_ITEM_TIME_IV, DRAWABLE_BLACK_TIME);
                    holder.setTextColor(RES_ID_LIST_ITEM_TIME_TV, Color.BLACK);
                    holder.setImg(RES_ID_LIST_ITEM_ENTER_IV, DRAWABLE_BLACK_ANCHOR);
                } else {
                    //票
                    holder.setVisibility(RES_ID_LIST_ITEM_CARD_TYPE, View.GONE);
                    holder.setBg(RES_ID_LIST_ITEM_PANEL, DRAWABLE_BLACK_BG);

                    holder.setBg(RES_ID_LIST_ITEM_LINE, DRAWABLE_YELLOW_LINE);
                    holder.setImg(RES_ID_LIST_ITEM_CARD_TYPE_ICON, DRAWABLE_WHITE_TICKET);
                    holder.setTextColor(RES_ID_LIST_ITEM_CARD_NAME, Color.WHITE);
                    holder.setImg(RES_ID_LIST_ITEM_ADDRESS_IV, DRAWABLE_WHITE_LOCATE);
                    holder.setTextColor(RES_ID_LIST_ITEM_ADDRESS_TV, Color.WHITE);
                    holder.setImg(RES_ID_LIST_ITEM_TIME_IV, DRAWABLE_WHITE_TIME);
                    holder.setTextColor(RES_ID_LIST_ITEM_TIME_TV, Color.WHITE);
                    holder.setImg(RES_ID_LIST_ITEM_ENTER_IV, DRAWABLE_WHITE_ANCHOR);
                }

                holder.setText(RES_ID_LIST_ITEM_CARD_NAME, data.getCardName());
                holder.setText(RES_ID_LIST_ITEM_ADDRESS_TV, data.getAddress());
                holder.setText(RES_ID_LIST_ITEM_CARD_TYPE, data.getTypeName());
                holder.setText(RES_ID_LIST_ITEM_TIME_TV, data.getEndTime());
                Glide.with($this).load(data.getUrl()).into((ImageView) holder.getView(RES_ID_LIST_ITEM_ICON));
                holder.getParentView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TicketDetailActivity.startActivity(context, data.getCard_id(), data.getAdmissionType());

                    }
                });
            }
        };
        listView.setAdapter(admissionAdapter);
    }

    @Override
    protected void initView() {
        super.initView();
        refreshLayout.setOnRefreshListener(this);
        getUserFacePhoto();
    }


    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        getCardInList();
        setRefresh(false);

    }

    private void setRefresh(boolean flag) {
        if (refreshLayout != null) {
            refreshLayout.setRefreshing(flag);
        }
    }

    @OnClick(RES_ID_FACE)
    public void toUploadFacePic() {
        FaceActivity.startActivity(this.getActivity());
    }

    //    检测面部识别是否录入
    private void getUserFacePhoto() {
        SignHelper signHelper = new SignHelper("");
        mAppHelper.apiService().getUserFacePhoto(signHelper.getToken()).enqueue(new BaseCallback<ApiResponse<FaceBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<FaceBean>> call, Response<ApiResponse<FaceBean>> response) {
                FaceBean bean = response.body().getData();
                if (TextUtils.isEmpty(bean.getFace1())) {
                    facePanel.setVisibility(View.VISIBLE);
                } else {
                    facePanel.setVisibility(View.GONE);
                }

            }

            @Override
            public void onTokenTimeOut() {
            }
        });
    }

    //    获取入场卡券列表
    private void getCardInList() {
        mAppHelper.apiService().getCardInList(pageNo + "").enqueue(new BaseCallback<ApiResponse<CardResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<CardResponseBean>> call, Response<ApiResponse<CardResponseBean>> response) {
                int total = Integer.parseInt(response.body().getData().getTotal());
                int currentPage = Integer.parseInt(response.body().getData().getCurrent_page());
                int perPage = Integer.parseInt(response.body().getData().getPer_page());
                if (total <= currentPage * perPage) {
                    refreshLayout.setDirection(SwipyRefreshLayoutDirection.TOP);
                } else {
                    refreshLayout.setDirection(SwipyRefreshLayoutDirection.BOTH);
                }
                if (pageNo == 1) {
                    admissionList.clear();
                }
                admissionList.addAll(AdmissionBean.dataChange(response.body().getData().getData()));
                admissionAdapter.notifyDataSetChanged();
            }
        });
    }

}
